//
//  RHCellRealDataModel.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/3/6.
//

#import "RHCellRealDataModel.h"
#import "RHDevPackModel.h"

@implementation RHCellRealDataModel
-(void)setPackArr:(NSArray *)packArr
{
    _packArr  = packArr;
    

}


-(void)setYValues:(NSArray *)yValues
{
    _yValues = yValues;
    _maxValue = [[yValues valueForKeyPath:@"@max.doubleValue"] doubleValue];
    _minValue = [[yValues valueForKeyPath:@"@min.doubleValue"] doubleValue];
    
    
    //最大值
    NSString *maxValue = [NSDecimalNumber decimalStringWithDouble:_maxValue];
    //电压最小值
    NSString *minValue = [NSDecimalNumber decimalStringWithDouble:_minValue];
    //最大压差
    CGFloat dif_value = _maxValue - _minValue;
    NSString *difStr = [NSDecimalNumber decimalStringWithDouble:dif_value];
    
    //最高位置  packIndex -
    __block  NSString *max_index = @"";
    //最低位置
    __block NSString *min_index = @"";
    
    [_packArr enumerateObjectsUsingBlock:^(RHDevPackModel  *_Nonnull packModel, NSUInteger index, BOOL * _Nonnull stop) {
        NSArray *arr;
        
        if([self.sectionTitle isEqualToString:@"电压统计".rh_localized]){
            arr = packModel.volt;
        }else{
            arr = packModel.temp;
        }
        
        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if([ValidNullData(obj) doubleValue] == _maxValue && (max_index.length == 0)){
                max_index = [NSString stringWithFormat:@"%ld-%ld",index+1,idx+1];
            }
            
            if([ValidNullData(obj) doubleValue] == _minValue && (min_index.length == 0)){
                min_index = [NSString stringWithFormat:@"%ld-%ld",index+1,idx+1];;
            }
        }];
    }];
    
    
    //最大值在总数据中的位置
    [_yValues enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if([obj doubleValue] == _maxValue){
            _max_position = idx ;
            *stop = YES;
        }
    }];
    
    [_yValues enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if([obj doubleValue] == _minValue){
            _min_position = idx ;
            *stop = YES;
        }
    }];
    
    _pointDataArr = @[maxValue,minValue,difStr,max_index,min_index];
}
@end
